<?php
if(!function_exists('isMobile'))
{
    function isMobile($telephone)
    {
        $pattern = "/^(1)[0-9]{10}$/";
        //var_dump(preg_match($pattern, $telephone));
        if (!empty($telephone) && preg_match($pattern, $telephone))
        {
            return true;
        }

        return false;
    }
}

function getPicUniqueName()
{
    return md5(time().str_random(16));
}

/**
 * Json方式 查询订单物流轨迹
 */
function getOrderTracesByJson($express_code, $express_no, $order_code = ''){
    $requestData= "{'OrderCode':'{$order_code}','ShipperCode':'{$express_code}','LogisticCode':'{$express_no}'}";

    $datas = array(
        'EBusinessID' => '1363157',
        'RequestType' => '1002',
        'RequestData' => urlencode($requestData) ,
        'DataType' => '2',
    );
    $datas['DataSign'] = kdniao_encrypt($requestData, 'f426aae4-89fa-4a90-863c-08c96328b7f6');
    $result=sendPost('http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx', $datas);

    return $result;
}

/**
 * 电商Sign签名生成
 * @param data 内容
 * @param appkey Appkey
 * @return DataSign签名
 */
function kdniao_encrypt($data, $appkey)
{
    return urlencode(base64_encode(md5($data.$appkey)));
}

/**
 *  post提交数据
 * @param  string $url 请求Url
 * @param  array $datas 提交的数据
 * @return url响应返回的html
 */
function sendPost($url, $datas)
{
    $temps = array();
    foreach ($datas as $key => $value) {
        $temps[] = sprintf('%s=%s', $key, $value);
    }
    $post_data = implode('&', $temps);
    $url_info = parse_url($url);
    if(empty($url_info['port']))
    {
        $url_info['port']=80;
    }
    $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
    $httpheader.= "Host:" . $url_info['host'] . "\r\n";
    $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
    $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
    $httpheader.= "Connection:close\r\n\r\n";
    $httpheader.= $post_data;
    $fd = fsockopen($url_info['host'], $url_info['port']);
    fwrite($fd, $httpheader);
    $gets = "";
    $headerFlag = true;
    while (!feof($fd)) {
        if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
            break;
        }
    }
    while (!feof($fd)) {
        $gets.= fread($fd, 128);
    }
    fclose($fd);

    return $gets;
}

function replace_intro($intro)
{
    $intro = str_replace("\r\n",'',strip_tags($intro));
    $intro = str_replace("&ldquo;",'“',$intro);
    $intro = str_replace("&rdquo;",'”',$intro);
    $intro = str_replace("&hellip;",'…',$intro);
    $intro = str_replace("&middot;",'·',$intro);
    $intro = str_replace("&mdash;",'—',$intro);
    $intro = str_replace("&nbsp;",'',$intro);

    return $intro;
}



